package com.study;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zhuzi
 * 2023/9/15 16:38
 */
@Slf4j
public class DeflaterUtils {


    public static void main1(String[] args) {
        String str = "MH2399999";
        for (int i = 0; i <= 9; i++) {
            String myxor = myxor(str, i);
            String myxor1 = myxor(myxor, i);
            System.out.println(StrUtil.format("{} {} 使用数字{}", myxor, myxor1, i));
        }
    }

    public static void main(String[] args) {

        String substring = StringUtils.substring(STR, -1, -2);
        System.out.println(substring);
        String mh4587961 = encry("MH");
        System.out.println(mh4587961);

        String s = decry(mh4587961);
        System.out.println(s);
    }



    private static void demo(int i) {
        String str = "1234567890QWERTYUIOPLKJHGFDSAZXCVBNM";
        char[] chars = str.toCharArray();

        for (char aChar : chars) {
            int result = aChar ^ i;
            System.out.printf((char) result + " ");
        }
        System.out.println();
    }
    public static final String STR = "HJKLZXCVBNM";
    private static String encry(String str) {
        StringBuffer sb = new StringBuffer();
        for (String s : str.split("")) {
            int indexOf = StringUtils.indexOf(STR, s);
            int i = indexOf + 2;
            if (i >= STR.length()) {
                i = i - STR.length();
            }
            String substring = StringUtils.substring(STR, i, i + 1);
            sb.append(substring);
        }
        return sb.toString();
    }

    private static String decry(String str) {
        StringBuffer sb = new StringBuffer();
        for (String s : str.split("")) {
            int i = StringUtils.indexOf(STR, s) - 2;
            if (i < 0) {
                i = STR.length() + i;
            }
            String substring = StringUtils.substring(STR, i, i + 1);
            sb.append(substring);
        }
        return sb.toString();
    }

    public static String myxor(String str, int pwd) {
        char[] chars = (str).toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            int result = chars[i] ^ (pwd);
            char old = (char) result;
            sb.append(old);
        }
        return StrUtil.reverse(sb.toString());
    }

    public static String xor(String data, String password) {
        //异或加密
        byte b1[] = data.getBytes();
        byte b2[] = password.getBytes();
        byte longbytes[], shortbytes[];
        if (b1.length >= b2.length) {
            longbytes = b1;
            shortbytes = b2;
        } else {
            longbytes = b2;
            shortbytes = b1;
        }
        byte xorstr[] = new byte[longbytes.length];
        int i = 0;
        for (; i < shortbytes.length; i++) {
            xorstr[i] = (byte) (shortbytes[i] ^ longbytes[i]);
        }
        for (; i < longbytes.length; i++) {
            xorstr[i] = longbytes[i];
        }
        return new String(xorstr);
    }
}

